using System;
using System.Collections.Generic;

namespace Matrix.Configuration.Events
{
    /// <summary>
    /// 配置变更本地事件
    /// </summary>
    public class ConfigurationChangedLocalEvent
    {
        /// <summary>
        /// 配置变更事件数据
        /// </summary>
        public ConfigurationChangedEto EventData { get; set; } = new();

        /// <summary>
        /// 事件处理上下文
        /// </summary>
        public ConfigurationEventContext Context { get; set; } = new();

        /// <summary>
        /// 构造函数
        /// </summary>
        public ConfigurationChangedLocalEvent()
        {
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="eventData">配置变更事件数据</param>
        public ConfigurationChangedLocalEvent(ConfigurationChangedEto eventData)
        {
            EventData = eventData ?? throw new ArgumentNullException(nameof(eventData));
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="eventData">配置变更事件数据</param>
        /// <param name="context">事件处理上下文</param>
        public ConfigurationChangedLocalEvent(ConfigurationChangedEto eventData, ConfigurationEventContext context)
        {
            EventData = eventData ?? throw new ArgumentNullException(nameof(eventData));
            Context = context ?? throw new ArgumentNullException(nameof(context));
        }
    }

    /// <summary>
    /// 配置批量变更本地事件
    /// </summary>
    public class ConfigurationBatchChangedLocalEvent
    {
        /// <summary>
        /// 批量配置变更事件数据
        /// </summary>
        public ConfigurationBatchChangedEto BatchEventData { get; set; } = new();

        /// <summary>
        /// 事件处理上下文
        /// </summary>
        public ConfigurationEventContext Context { get; set; } = new();

        /// <summary>
        /// 构造函数
        /// </summary>
        public ConfigurationBatchChangedLocalEvent()
        {
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="batchEventData">批量配置变更事件数据</param>
        public ConfigurationBatchChangedLocalEvent(ConfigurationBatchChangedEto batchEventData)
        {
            BatchEventData = batchEventData ?? throw new ArgumentNullException(nameof(batchEventData));
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="batchEventData">批量配置变更事件数据</param>
        /// <param name="context">事件处理上下文</param>
        public ConfigurationBatchChangedLocalEvent(ConfigurationBatchChangedEto batchEventData, ConfigurationEventContext context)
        {
            BatchEventData = batchEventData ?? throw new ArgumentNullException(nameof(batchEventData));
            Context = context ?? throw new ArgumentNullException(nameof(context));
        }
    }

    /// <summary>
    /// 配置缓存失效本地事件
    /// </summary>
    public class ConfigurationCacheInvalidatedLocalEvent
    {
        /// <summary>
        /// 缓存键列表
        /// </summary>
        public List<string> CacheKeys { get; set; } = new();

        /// <summary>
        /// 配置键
        /// </summary>
        public string ConfigurationKey { get; set; } = string.Empty;

        /// <summary>
        /// 配置命名空间
        /// </summary>
        public string Namespace { get; set; } = string.Empty;

        /// <summary>
        /// 缓存失效原因
        /// </summary>
        public string Reason { get; set; } = string.Empty;

        /// <summary>
        /// 是否为分布式缓存
        /// </summary>
        public bool IsDistributedCache { get; set; }

        /// <summary>
        /// 事件发生时间
        /// </summary>
        public DateTime EventTime { get; set; }

        /// <summary>
        /// 租户标识
        /// </summary>
        public Guid? TenantId { get; set; }
    }

    /// <summary>
    /// 配置验证本地事件
    /// </summary>
    public class ConfigurationValidationLocalEvent
    {
        /// <summary>
        /// 配置键
        /// </summary>
        public string Key { get; set; } = string.Empty;

        /// <summary>
        /// 配置命名空间
        /// </summary>
        public string Namespace { get; set; } = string.Empty;

        /// <summary>
        /// 配置值
        /// </summary>
        public string? Value { get; set; }

        /// <summary>
        /// 值类型
        /// </summary>
        public string ValueType { get; set; } = "string";

        /// <summary>
        /// 验证结果
        /// </summary>
        public ConfigurationValidationResult ValidationResult { get; set; } = new();

        /// <summary>
        /// 验证触发者
        /// </summary>
        public string TriggeredBy { get; set; } = string.Empty;

        /// <summary>
        /// 验证时间
        /// </summary>
        public DateTime ValidationTime { get; set; }

        /// <summary>
        /// 租户标识
        /// </summary>
        public Guid? TenantId { get; set; }
    }

    /// <summary>
    /// 配置事件上下文
    /// </summary>
    public class ConfigurationEventContext
    {
        /// <summary>
        /// 事件来源服务
        /// </summary>
        public string SourceService { get; set; } = string.Empty;

        /// <summary>
        /// 事件来源实例
        /// </summary>
        public string SourceInstance { get; set; } = string.Empty;

        /// <summary>
        /// 请求ID
        /// </summary>
        public string? RequestId { get; set; }

        /// <summary>
        /// 跟踪ID
        /// </summary>
        public string? TraceId { get; set; }

        /// <summary>
        /// 用户ID
        /// </summary>
        public string? UserId { get; set; }

        /// <summary>
        /// 用户名
        /// </summary>
        public string? UserName { get; set; }

        /// <summary>
        /// 租户ID
        /// </summary>
        public Guid? TenantId { get; set; }

        /// <summary>
        /// 客户端IP地址
        /// </summary>
        public string? ClientIpAddress { get; set; }

        /// <summary>
        /// 用户代理
        /// </summary>
        public string? UserAgent { get; set; }

        /// <summary>
        /// 关联事件ID列表
        /// </summary>
        public List<Guid> CorrelationEventIds { get; set; } = new();

        /// <summary>
        /// 事件元数据
        /// </summary>
        public Dictionary<string, object> Metadata { get; set; } = new();

        /// <summary>
        /// 事件发生时间
        /// </summary>
        public DateTime EventTime { get; set; } = DateTime.UtcNow;
    }

    /// <summary>
    /// 配置验证结果
    /// </summary>
    public class ConfigurationValidationResult
    {
        /// <summary>
        /// 是否验证通过
        /// </summary>
        public bool IsValid { get; set; }

        /// <summary>
        /// 验证错误信息列表
        /// </summary>
        public List<string> Errors { get; set; } = new();

        /// <summary>
        /// 验证警告信息列表
        /// </summary>
        public List<string> Warnings { get; set; } = new();

        /// <summary>
        /// 验证详情
        /// </summary>
        public Dictionary<string, object> Details { get; set; } = new();

        /// <summary>
        /// 验证耗时（毫秒）
        /// </summary>
        public long ElapsedMilliseconds { get; set; }

        /// <summary>
        /// 添加错误信息
        /// </summary>
        public void AddError(string error)
        {
            Errors.Add(error);
        }

        /// <summary>
        /// 添加警告信息
        /// </summary>
        public void AddWarning(string warning)
        {
            Warnings.Add(warning);
        }

        /// <summary>
        /// 合并其他验证结果
        /// </summary>
        public void Merge(ConfigurationValidationResult other)
        {
            if (other != null)
            {
                Errors.AddRange(other.Errors);
                Warnings.AddRange(other.Warnings);
            }
        }
    }

    /// <summary>
    /// 配置订阅统计信息
    /// </summary>
    public class ConfigurationSubscriptionStatistics
    {
        /// <summary>
        /// 总订阅数
        /// </summary>
        public int TotalSubscriptions { get; set; }

        /// <summary>
        /// 按键统计订阅数
        /// </summary>
        public Dictionary<string, int> SubscriptionsByKey { get; set; } = new();

        /// <summary>
        /// 按命名空间统计订阅数
        /// </summary>
        public Dictionary<string, int> SubscriptionsByNamespace { get; set; } = new();

        /// <summary>
        /// 按类型统计订阅数
        /// </summary>
        public Dictionary<Type, int> SubscriptionsByType { get; set; } = new();

        /// <summary>
        /// 最后通知时间
        /// </summary>
        public DateTime? LastNotificationTime { get; set; }

        /// <summary>
        /// 总通知数
        /// </summary>
        public long TotalNotifications { get; set; }
    }
}